package com.zx.tv.ui;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.animation.AnimationUtils;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.zx.base.ctrl.Constant;
import com.zx.base.ctrl.KeyTable;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.URLHelper;
import com.zx.base.lazylist.ImageLoader;
import com.zx.base.util.DPIExchange;
import com.zx.tv.R;
import com.zx.tv.app.NewTVApp;
import com.zx.tv.entity.Content;

public class NewsContent extends Activity {
	private ImageView newscontent_adv_0, newscontent_adv_1;
	private ImageView newscontent_pic;
	private ImageLoader imageloader;
	private TextView newscontent_content;
	private ViewFlipper newscontent_flipper;
	private TextView newscontent_weather_1, newscontent_weather_2,
			newscontent_weather_3;
	private RelativeLayout top, bottom, substance;
	private String newsid = "";
	private ScrollView newscontent_scroll;
	private Content content;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newscontent);
		newsid = getIntent().getStringExtra("contentid");
		newscontent_flipper = (ViewFlipper) findViewById(R.id.newscontent_flipper);
		newscontent_scroll = (ScrollView) findViewById(R.id.newscontent_scroll);
		imageloader = ((NewTVApp) getApplication()).imageloader;
		top = (RelativeLayout) findViewById(R.id.top);
		bottom = (RelativeLayout) findViewById(R.id.bottom);
		substance = (RelativeLayout) findViewById(R.id.substance);
		newscontent_adv_0 = (ImageView) findViewById(R.id.newscontent_adv_0);
		newscontent_adv_1 = (ImageView) findViewById(R.id.newscontent_adv_1);
		DPIExchange dpi = new DPIExchange(this);
		LayoutParams toplp = new LayoutParams(LayoutParams.MATCH_PARENT,
				dpi.heightChange(100));
		top.setLayoutParams(toplp);
		LayoutParams headerlp = new LayoutParams(LayoutParams.MATCH_PARENT,
				dpi.heightChange(100));
		headerlp.setMargins(dpi.widthChange(375), 0, dpi.widthChange(375), 0);

		LayoutParams flipperlp = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		flipperlp.setMargins(dpi.widthChange(375), 0, dpi.widthChange(375), 0);
		newscontent_flipper.setLayoutParams(flipperlp);

		LayoutParams bottomlp = new LayoutParams(LayoutParams.MATCH_PARENT,
				dpi.heightChange(100));
		bottomlp.alignWithParent = true;
		bottomlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		bottom.setLayoutParams(bottomlp);
		LayoutParams rp = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		rp.setMargins(dpi.widthChange(375), dpi.heightChange(200),
				dpi.widthChange(375), dpi.heightChange(200));
		substance.setLayoutParams(rp);

		LayoutParams advl = new LayoutParams(dpi.widthChange(200),
				dpi.heightChange(680));
		advl.setMargins(20, dpi.heightChange(200), 0, 0);
		advl.alignWithParent = true;
		advl.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		newscontent_adv_0.setLayoutParams(advl);
		LayoutParams advr = new LayoutParams(dpi.widthChange(200),
				dpi.heightChange(680));
		advr.setMargins(0, dpi.heightChange(200), 20, 0);
		advr.alignWithParent = true;
		advr.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		newscontent_adv_1.setLayoutParams(advr);

		newscontent_pic = (ImageView) findViewById(R.id.newscontent_pic);
		newscontent_content = (TextView) findViewById(R.id.newscontent_text);
		newscontent_weather_1 = (TextView) findViewById(R.id.newscontent_weather_1);
		newscontent_weather_2 = (TextView) findViewById(R.id.newscontent_weather_2);
		newscontent_weather_3 = (TextView) findViewById(R.id.newscontent_weather_3);

		newscontent_flipper.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_up_in));
		newscontent_flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_up_out));
		newscontent_flipper.startFlipping();
		new InfoTask().execute(URLHelper.mPrefix);
	}

	class InfoTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {

			Map<String, String> map = new HashMap<String, String>();
			map.put("act", "newsContent");
			map.put("newsid", newsid);
			map.put("areanum", Constant.CITYCODE);
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			if (Constant.debug)
				Log.d("TAG", "login result = " + result);
			if (result == null || result.equals("timeout") || result == "") {
				Toast toast = Toast.makeText(NewsContent.this, "网络超时。",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else {
				// 发送解析数据 消息
				parseJson(result);
			}

		}
	}

	// 解析获取的服务器json数据
	private void parseJson(String str) {
		try {
			JSONObject result = new JSONObject(str);
			String succeed = result.getString("succeed");
			if (succeed.equals("000")) {
				content = new Content();
				JSONArray datalist = result.getJSONArray("dataList");
				for (int i = 0; i < datalist.length(); i++) {
					JSONObject one = datalist.getJSONObject(i);
					content.setTitle(one.getString("title"));
					content.setSubhead(one.getString("subhead"));
					content.setPtime(one.getString("ptime"));
					content.setAuthor(one.getString("author"));
					 content.setContent(one.getString("content"));
					content.setImg(one.getString("imgurl"));
				}

				imageloader.DisplayImage(
						URLHelper.mPrefixImg + content.getImg(),
						newscontent_pic, R.drawable.loading);

				newscontent_content
						.setText(Html.fromHtml(content.getContent()));

				JSONArray advertisList = result.getJSONArray("advList");
				for (int i = 0; i < advertisList.length(); i++) {
					JSONObject one = advertisList.getJSONObject(i);
					int position = one.getInt("position");
					switch (position) {
					case 0:
						imageloader.DisplayImage(
								URLHelper.mPrefixImg + one.getString("imgurl"),
								newscontent_adv_0, R.drawable.loading);
						break;
					case 1:
						imageloader.DisplayImage(
								URLHelper.mPrefixImg + one.getString("imgurl"),
								newscontent_adv_1, R.drawable.loading);
						break;

					default:
						break;
					}
				}
				JSONObject weather = result.getJSONObject("weather");
				newscontent_weather_1.setText(Html.fromHtml(weather
						.getJSONObject("weather1").getString("msg")));
				newscontent_weather_2.setText(Html.fromHtml(weather
						.getJSONObject("weather2").getString("msg")));
				newscontent_weather_3.setText(Html.fromHtml(weather
						.getJSONObject("weather3").getString("msg")));
				content.getHTMLContent();
			} else {

			}
		} catch (Exception e) {
			Log.d("TAG", "" + e.toString());
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyTable.KEY_ENTER) {
			Intent intent = new Intent(NewsContent.this, Detail.class);
			intent.putExtra("imgId", content.getImg());
			startActivity(intent);
			return true;
		} else if (keyCode == KeyTable.KEY_DOWN || keyCode == KeyTable.KEY_UP) {
			newscontent_scroll.arrowScroll(keyCode);
		}
		return super.onKeyDown(keyCode, event);
	}
}